package com.heima.controller;

import com.heima.common.Result;
import com.heima.pojo.sku_class.AliyunOssProperties;
import com.heima.utils.AliyunOSSUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
@Slf4j
@RequestMapping("/api/vm-service/sku/fileUpload")
public class ImgUploadController {
    @Autowired
    private AliyunOssProperties aliyunOssProperties;
    /**
     * 图片上传
     */
    @PostMapping
    @ApiOperation("图片上传")
    public String upload(MultipartFile fileName) throws Exception {
        log.info("文件上传，文件名为：{}", fileName);
        log.info("校验阿里云 {},{}",aliyunOssProperties.getEndpoint(),aliyunOssProperties.getBucketName());
        String extName = fileName.getOriginalFilename().substring(fileName.getOriginalFilename().lastIndexOf("."));
        String url = AliyunOSSUtils.upload(aliyunOssProperties.getEndpoint(), aliyunOssProperties.getBucketName(), fileName.getBytes(), extName);
        return url;
    }
}
